package com.plian.system.service.pf.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.plian.Tools.CollectionUtil;
import com.plian.system.dto.pf.PerformanceReportDTO;
import com.plian.system.entity.bpa.BpaIndustryStandardConfig;
import com.plian.system.entity.bpa.BusinessAssessment;
import com.plian.system.entity.bpa.BusinessAssessmentGear;
import com.plian.system.entity.bpa.BusinessAssessmentScore;
import com.plian.system.entity.fr.indicators.FinanceIndicators;
import com.plian.system.entity.fr.indicators.IndicatorsValue;
import com.plian.system.entity.pf.*;
import com.plian.system.entity.sys.Org;
import com.plian.system.mapper.bpa.BpaFinancialPerformanceGearMapper;
import com.plian.system.mapper.bpa.BpaIndustryCreditConfigMapper;
import com.plian.system.mapper.fr.indicators.FinanceIndicatorsMapper;
import com.plian.system.mapper.fr.indicators.IndicatorsValueMapper;
import com.plian.system.mapper.pf.BusinessIndicatorsMapper;
import com.plian.system.properties.GzwProperties;
import com.plian.system.service.bpa.*;
import com.plian.system.service.pf.*;
import com.plian.system.service.sys.orgzation.IOrgService;
import com.plian.system.vo.pf.AppraisalIndexVO;
import com.plian.system.vo.pf.BusinessIndicatorsReportVO;
import com.plian.system.vo.pf.BusinessMonthReportVO;
import com.plian.system.vo.pf.PerformanceReportVO;
import com.plian.system.vo.report.HeaderVO;
import com.plian.system.vo.report.ReportReturnVO;
import com.plian.system.vo.report.ReportVO;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: Take-off
 * @Description: TODO 履职成效-报表 实现
 * @Date: 2023/2/3 4:46 PM
 * @Version: 1.0
 **/
@Service
@AllArgsConstructor
@Slf4j
public class PerformanceReportServiceImpl implements IPerformanceReportService {

    private final IAppraisalIndexService appraisalIndexService;

    private final IPerformanceScoreService performanceScoreService;

    private final IPerformanceScoreMethodService scoreMethodService;

    private final IBaseEvaluationScoreService evaluationScoreService;

    private final IBaseOfficeScoreService officeScoreService;

    private final IBaseAssessScoreService assessScoreService;

    private final IEvaluationScoreDetailService scoreDetailService;

    private final IBaseBusinessIndicatorsService baseBusinessIndicatorsService;

    private final IBusinessIndicatorsService businessIndicatorsService;

    private final ITaskIndicatorsService taskIndicatorsService;

    private final ITaskAnnualService taskAnnualService;

    private final IBaseBusinessAuditService baseBusinessAuditService;

    private final IBusinessAuditService businessAuditService;

    private final IBaseBusinessAnnualService baseBusinessAnnualService;

    private final IBusinessAnnualService businessAnnualService;

    private final FinanceIndicatorsMapper indicatorsMapper;

    private final IndicatorsValueMapper indicatorsValueMapper;

    private final IOperatingPerformanceService operatingPerformanceService;


    private BpaFinancialPerformanceGearMapper performanceGearMapper;

    private BpaIndustryCreditConfigMapper industryCreditConfigMapper;

    private final IBaseBusinessProgressService baseBusinessProgressService;

    private final IBusinessProgressService businessProgressService;

    @Resource
    private BusinessIndicatorsMapper businessIndicatorsMapper;

    private final IBusinessAssessmentService businessAssessmentService;

    private final IBusinessAssessmentScoreService businessAssessmentScoreService;

    private final IBusinessAssessmentScoreDynamicService businessAssessmentScoreDynamicService;

    private final IBusinessAssessmentGearService businessAssessmentGearService;

    private final IBusinessAssessmentGearDynamicService businessAssessmentGearDynamicService;

    private final IMainAuditService mainAuditService;

    private final IMainBusinessService mainBusinessService;

    private final IMainAnnualService mainAnnualService;

    private final IAuditAssetsService auditAssetsService;

    private final IOrgService orgService;

    private final GzwProperties gzwProperties;


    @Override
    public ReportReturnVO getPerformanceReport(PerformanceReportDTO dto) {
        //履职成效 考核得分
        List<PerformanceReportVO> performanceReportVOS = getPerformanceReportList(dto);

        //小计得分
        PerformanceReportVO countValue = getCountValue(performanceReportVOS).get(0);
        performanceReportVOS.add(countValue);
        //报表头部
        ReportVO reportVO = new ReportVO();
        reportVO
                .addHeader(HeaderVO.builder().key("firstIndex").title("一级指标").build())
                .addHeader(HeaderVO.builder().key("secondaryIndex").title("二级指标").build())
                .addHeader(HeaderVO.builder().key("threeIndex").title("三级指标").build())
                .addHeader(HeaderVO.builder().key("content").title("评价内容").build())
                .addHeader(HeaderVO.builder().key("weight").title("权重").build())
                .addHeader(HeaderVO.builder().key("enterpriseScore").title("企业自评分").build())
                .addHeader(HeaderVO.builder().key("enterpriseResult").title("折合得分").build())
                .addHeader(HeaderVO.builder().key("officeScore").title("责任处室评分").build())
                .addHeader(HeaderVO.builder().key("officeResult").title("折合得分").build())
                .addHeader(HeaderVO.builder().key("assetScore").title("国资评分").build())
                .addHeader(HeaderVO.builder().key("assetResult").title("折合得分").build())
        ;
        reportVO.setData(performanceReportVOS);
        ReportReturnVO reportReturnVO = new ReportReturnVO();
        reportReturnVO.setGrids(reportVO);
        return reportReturnVO;
    }

    @Override
    public ReportReturnVO getAllCountReport(PerformanceReportDTO dto) {
        List<BusinessIndicatorsReportVO> businessIndicatorsReportVOS = new ArrayList<>();

        List<BusinessIndicatorsReportVO> sumValueVOS = new ArrayList<>();

        //履职成效 考核得分
        List<PerformanceReportVO> performanceReportVOS = getPerformanceReportList(dto);
        if (CollectionUtil.isNotEmpty(performanceReportVOS)) {
            performanceReportVOS.forEach(performanceReportVO -> {
                businessIndicatorsReportVOS.add(performanceToBusiness(performanceReportVO));
                sumValueVOS.add(performanceToBusiness(performanceReportVO));
            });
        }
        //履职成效 小计得分
        PerformanceReportVO countValue = getCountValue(performanceReportVOS).get(0);
        businessIndicatorsReportVOS.add(performanceToBusiness(countValue));
        //经营业绩 考核得分
        List<BusinessIndicatorsReportVO> businessIndicatorsReports = getBusinessIndicatorsReport(dto);
        businessIndicatorsReportVOS.addAll(businessIndicatorsReports);
        //经营业绩 小计得分
        businessIndicatorsReportVOS.add(getBusinessCountValue(businessIndicatorsReports).get(0));

        //年度考核得分
        sumValueVOS.addAll(businessIndicatorsReports);
        BusinessIndicatorsReportVO sumValue = getBusinessCountValue(sumValueVOS).get(0);
        sumValue.setFirstIndex("年度考核得分");
        businessIndicatorsReportVOS.add(sumValue);

        //报表头部
        ReportVO reportVO = new ReportVO();
        List<HeaderVO> indexs = new ArrayList<>();
        indexs.add(HeaderVO.builder().key("firstIndex").title("一级指标").build());
        indexs.add(HeaderVO.builder().key("secondaryIndex").title("二级指标").build());
        indexs.add(HeaderVO.builder().key("threeIndex").title("三级指标").build());

        List<HeaderVO> enterprises = new ArrayList<>();
        enterprises.add(HeaderVO.builder().key("enterpriseScore").title("完成值").build());
        enterprises.add(HeaderVO.builder().key("enterpriseItem").title("分项得分").build());
        enterprises.add(HeaderVO.builder().key("enterpriseResult").title("折合得分").build());

        List<HeaderVO> offices = new ArrayList<>();
        offices.add(HeaderVO.builder().key("officeScore").title("完成值").build());
        offices.add(HeaderVO.builder().key("officeItem").title("分项得分").build());
        offices.add(HeaderVO.builder().key("officeResult").title("折合得分").build());

        List<HeaderVO> assets = new ArrayList<>();
        assets.add(HeaderVO.builder().key("assetScore").title("完成值").build());
        assets.add(HeaderVO.builder().key("assetItem").title("分项得分").build());
        assets.add(HeaderVO.builder().key("assetResult").title("折合得分").build());

        reportVO
                .addHeader(HeaderVO.builder().title("指标名称").child(indexs).build())
                .addHeader(HeaderVO.builder().title("指标权重").key("weight").build())
                .addHeader(HeaderVO.builder().title("基准值").key("baseline").build())
                .addHeader(HeaderVO.builder().title("激励目标值").key("target").build())
                .addHeader(HeaderVO.builder().title("基本目标值").key("basicTarget").build())
                .addHeader(HeaderVO.builder().title("企业上报").child(enterprises).build())
                .addHeader(HeaderVO.builder().title("处室评分（审计认定）").child(offices).build())
                .addHeader(HeaderVO.builder().title("国资审核").child(assets).build())
        ;

        reportVO.setData(businessIndicatorsReportVOS);
        ReportReturnVO reportReturnVO = new ReportReturnVO();
        reportReturnVO.setGrids(reportVO);
        return reportReturnVO;


    }

    @Override
    public ReportReturnVO getOrgCountReport(PerformanceReportDTO dto) {
        List<Map<String, Object>> resultMaps = new ArrayList<>();
        //计算总和
        List<BusinessIndicatorsReportVO> sumValueVOS = new ArrayList<>();
        //履职成效 考核得分
        List<PerformanceReportVO> performanceReportVOS = getPerformanceReportList(dto);
        if (CollectionUtil.isNotEmpty(performanceReportVOS)) {
            performanceReportVOS.forEach(performanceReportVO -> {
                sumValueVOS.add(performanceToBusiness(performanceReportVO));
            });
        }
        //履职成效小计得分
        List<PerformanceReportVO> countValues = getCountValue(performanceReportVOS);
        //根据三级指标
        Map<String, List<PerformanceReportVO>> mayByIndexName = performanceReportVOS.stream().collect(Collectors.groupingBy(PerformanceReportVO::getGroupFlag));
        //13家
        List<Org> orgList = getOrgList(dto.getOrgId());
        //履职成效
        List<Map<String, Object>> sortPerformMaps = new ArrayList<>();
        for (String indexName : mayByIndexName.keySet()) {
            Map<String, Object> map = new HashMap<>();
            List<PerformanceReportVO> indexReportVOS = mayByIndexName.get(indexName);
            indexNameToMap(map, indexReportVOS.get(0));
            for (Org org : orgList) {
                List<PerformanceReportVO> performanceReportVOList = indexReportVOS.stream().filter(m -> org.getId().equals(m.getOrgId())).collect(Collectors.toList());
                scoreToMap(map, performanceReportVOList, org.getName());
            }
            sortPerformMaps.add(map);
        }
        if (CollectionUtil.isNotEmpty(sortPerformMaps)) {
            sortPerformMaps = sortPerformMaps.stream().sorted(Comparator.comparing(m -> m.get("sort").toString())).collect(Collectors.toList());
        }
        resultMaps.addAll(sortPerformMaps);


        Map<String, Object> performCount = new HashMap<>();
        performCount.put("firstIndex", "履职成效小计得分");
        for (Org org : orgList) {
            List<PerformanceReportVO> performanceReportVOList = countValues.stream().filter(m -> org.getId().equals(m.getOrgId())).collect(Collectors.toList());
            scoreToMap(performCount, performanceReportVOList, org.getName());
        }
        resultMaps.add(performCount);
        //经营业绩 考核得分
        List<Map<String, Object>> sortBusMaps = new ArrayList<>();
        List<BusinessIndicatorsReportVO> businessIndicatorsReports = getBusinessIndicatorsReport(dto);
        //根据三级指标
        Map<String, List<BusinessIndicatorsReportVO>> mayByBusName = businessIndicatorsReports.stream().collect(Collectors.groupingBy(PerformanceReportVO::getGroupFlag));

        for (String indexName : mayByBusName.keySet()) {
            Map<String, Object> map = new HashMap<>();
            List<BusinessIndicatorsReportVO> indexReportVOS = mayByBusName.get(indexName);
            indexNameToMap(map, indexReportVOS.get(0));
            for (Org org : orgList) {
                List<PerformanceReportVO> reportOptional = indexReportVOS.stream().filter(m -> org.getId().equals(m.getOrgId())).collect(Collectors.toList());
                scoreToMap(map, reportOptional, org.getName());
            }
            sortBusMaps.add(map);
        }
        if (CollectionUtil.isNotEmpty(sortBusMaps)) {
            sortBusMaps = sortBusMaps.stream().sorted(Comparator.comparing(m -> m.get("sort").toString())).collect(Collectors.toList());
        }
        resultMaps.addAll(sortBusMaps);
        //经营业绩小计得分
        List<BusinessIndicatorsReportVO> businessCountValues = getBusinessCountValue(businessIndicatorsReports);
        Map<String, Object> busCount = new HashMap<>();
        busCount.put("firstIndex", "经营业绩小计得分");
        for (Org org : orgList) {
            List<PerformanceReportVO> reportOptional = businessCountValues.stream().filter(m -> org.getId().equals(m.getOrgId())).collect(Collectors.toList());
            scoreToMap(busCount, reportOptional, org.getName());
        }
        resultMaps.add(busCount);
        //年度考核得分
        sumValueVOS.addAll(businessIndicatorsReports);
        List<BusinessIndicatorsReportVO> sumCountValues = getBusinessCountValue(sumValueVOS);
        Map<String, Object> sumCount = new HashMap<>();
        sumCount.put("firstIndex", "年度考核得分");
        for (Org org : orgList) {
            List<PerformanceReportVO> reportOptional = sumCountValues.stream().filter(m -> org.getId().equals(m.getOrgId())).collect(Collectors.toList());
            scoreToMap(sumCount, reportOptional, org.getName());
        }
        resultMaps.add(sumCount);

        //考核头部
        ReportVO reportVO = makeHead(dto.getDataTypes(), orgList);
        reportVO.setData(resultMaps);
        ReportReturnVO reportReturnVO = new ReportReturnVO();
        reportReturnVO.setGrids(reportVO);
        return reportReturnVO;
    }

    /**
     * @Description: //TODO 组建报表头部
     * @Author: Take-off
     * @Date: 5:19 PM 2023/8/4
     * @Param: [dataTypes, orgList]
     * @return: com.plian.system.vo.report.ReportVO
     **/
    private ReportVO makeHead(List<Integer> dataTypes, List<Org> orgList) {
        ReportVO reportVO = new ReportVO();
        List<HeaderVO> indexs = new ArrayList<>();
        indexs.add(HeaderVO.builder().key("firstIndex").title("一级指标").build());
        indexs.add(HeaderVO.builder().key("secondaryIndex").title("二级指标").build());
        indexs.add(HeaderVO.builder().key("threeIndex").title("三级指标").build());
        reportVO.addHeader(HeaderVO.builder().title("指标名称").child(indexs).build());

        for (Org org : orgList) {
            String name = org.getName();
            List<HeaderVO> orgHead = getOrgHead(dataTypes, name);
            reportVO.addHeader(HeaderVO.builder().title(name).child(orgHead).build());
        }
        return reportVO;
    }

    /**
     * @Description: 组建企业的头部
     * @Author: Take-off
     * @Date: 5:19 PM 2023/8/4
     * @Param: [dataTypes, orgName]
     * @return: java.util.List<com.plian.system.vo.report.HeaderVO>
     **/
    private List<HeaderVO> getOrgHead(List<Integer> dataTypes, String orgName) {
        List<HeaderVO> orgData = new ArrayList<>();
        if (dataTypes.contains(1)) {
            orgData.add(HeaderVO.builder().key("enterpriseResult_" + orgName).title("企业上报").build());
        }
        if (dataTypes.contains(2)) {
            orgData.add(HeaderVO.builder().key("officeResult_" + orgName).title("审计审定").build());
        }
        if (dataTypes.contains(3)) {
            orgData.add(HeaderVO.builder().key("assetResult_" + orgName).title("国资审核").build());
        }
        return orgData;
    }

    /**
     * @Description: 放置比分
     * @Author: Take-off
     * @Date: 4:51 PM 2023/8/4
     * @Param: [map, tempReport, orgName]
     * @return: void
     **/
    private void scoreToMap(Map<String, Object> map, List<PerformanceReportVO> performanceReportVOList, String orgName) {
        BigDecimal enterpriseResult = BigDecimal.ZERO;
        BigDecimal officeResult = BigDecimal.ZERO;
        BigDecimal assetResult = BigDecimal.ZERO;
        if (CollectionUtil.isNotEmpty(performanceReportVOList)) {
            enterpriseResult = performanceReportVOList.stream().map(PerformanceReportVO::getEnterpriseResult).filter(m -> Optional.ofNullable(m).isPresent()).reduce(BigDecimal.ZERO, BigDecimal::add);
            officeResult = performanceReportVOList.stream().map(PerformanceReportVO::getOfficeResult).filter(m -> Optional.ofNullable(m).isPresent()).reduce(BigDecimal.ZERO, BigDecimal::add);
            assetResult = performanceReportVOList.stream().map(PerformanceReportVO::getAssetResult).filter(m -> Optional.ofNullable(m).isPresent()).reduce(BigDecimal.ZERO, BigDecimal::add);
        }
        map.put("enterpriseResult_" + orgName, enterpriseResult);
        map.put("officeResult_" + orgName, officeResult);
        map.put("assetResult_" + orgName, assetResult);
    }

    /**
     * @Description: 放置指标
     * @Author: Take-off
     * @Date: 4:42 PM 2023/8/4
     * @Param: [map, tempReport]
     * @return: void
     **/
    private void indexNameToMap(Map<String, Object> map, PerformanceReportVO tempReport) {
        map.put("firstIndex", tempReport.getFirstIndex());
        map.put("secondaryIndex", tempReport.getSecondaryIndex());
        map.put("threeIndex", tempReport.getThreeIndex());
        map.put("content", tempReport.getContent());
        map.put("weight", tempReport.getWeight());
        map.put("sort", tempReport.getSort());
    }


    private List<Org> getOrgList(String orgId) {
        List<Org> orgList = new ArrayList<>();
        if (Optional.ofNullable(orgId).isPresent()) {
            orgList.add(orgService.getById(orgId));
        } else {
            orgList = orgService.list(new LambdaQueryWrapper<Org>().ne(Org::getIsDel, 1).eq(Org::getIsMonitor, 1).eq(Org::getParentId, gzwProperties.getReportId()).orderByAsc(Org::getOrderNum));
        }
        return orgList;
    }

    /**
     * @Author: Take-off
     * @Description: //TODO 履职成效转变为汇总得分
     * @Date: 3:54 PM 2023/2/5
     * @Param: [performanceReportVO]
     * @return: com.plian.system.vo.pf.BusinessIndicatorsReportVO
     **/
    private BusinessIndicatorsReportVO performanceToBusiness(PerformanceReportVO performanceReportVO) {
        BusinessIndicatorsReportVO businessIndicatorsReportVO = new BusinessIndicatorsReportVO();
        businessIndicatorsReportVO.setFirstIndex(performanceReportVO.getFirstIndex());
        businessIndicatorsReportVO.setSecondaryIndex(performanceReportVO.getSecondaryIndex());
        businessIndicatorsReportVO.setThreeIndex(performanceReportVO.getThreeIndex());
        businessIndicatorsReportVO.setContent(performanceReportVO.getContent());
        businessIndicatorsReportVO.setWeight(performanceReportVO.getWeight());
        businessIndicatorsReportVO.setEnterpriseScore(performanceReportVO.getEnterpriseScore());
        businessIndicatorsReportVO.setEnterpriseResult(performanceReportVO.getEnterpriseResult());
        businessIndicatorsReportVO.setOfficeScore(performanceReportVO.getOfficeScore());
        businessIndicatorsReportVO.setOfficeResult(performanceReportVO.getOfficeResult());
        businessIndicatorsReportVO.setAssetScore(performanceReportVO.getAssetScore());
        businessIndicatorsReportVO.setAssetResult(performanceReportVO.getAssetResult());
        businessIndicatorsReportVO.setOrgId(performanceReportVO.getOrgId());
        businessIndicatorsReportVO.setOrgName(performanceReportVO.getOrgName());

        return businessIndicatorsReportVO;
    }

    /**
     * @Author: Take-off
     * @Description: //TODO 获取履职成效 考核得分
     * @Date: 3:48 PM 2023/2/5
     * @Param: [dto]
     * @return: java.util.List<com.plian.system.vo.pf.PerformanceReportVO>
     **/
    private List<PerformanceReportVO> getPerformanceReportList(PerformanceReportDTO dto) {
        List<PerformanceReportVO> performanceReportVOS = new ArrayList<>();

        //指标
        List<AppraisalIndexVO> allAppraisalVOS = appraisalIndexService.treeByTableCode("003").getList();
        //考评内容
        List<BasePerformanceEffect> effects = performanceScoreService.list(new LambdaQueryWrapper<BasePerformanceEffect>().eq(BasePerformanceEffect::getFillingYear, dto.getYear())
                .eq(BasePerformanceEffect::getIsLatest, 2));
        //评分办法
        List<PerformanceScoreMethod> scoreMethods = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(effects)) {
            scoreMethods = scoreMethodService.list(new LambdaQueryWrapper<PerformanceScoreMethod>().eq(PerformanceScoreMethod::getBasePerformanceId, effects.get(0).getId()));
        }
        //企业评分
        List<BaseEvaluationScore> allEvaluationScores = evaluationScoreService.list(new LambdaQueryWrapper<BaseEvaluationScore>().eq(BaseEvaluationScore::getFillingYear, dto.getYear())
        );
        //责任处室评分
        List<BaseOfficeScore> allOfficeScores = officeScoreService.list(new LambdaQueryWrapper<BaseOfficeScore>().eq(BaseOfficeScore::getFillingYear, dto.getYear())
        );
        //考核处评分
        List<BaseAssessScore> allAssessScores = assessScoreService.list(new LambdaQueryWrapper<BaseAssessScore>().eq(BaseAssessScore::getFillingYear, dto.getYear())
        );
        //评分细则
        List<Long> allScoreIds = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(allEvaluationScores)) {
            allScoreIds.addAll(allEvaluationScores.stream().map(BaseEvaluationScore::getId).collect(Collectors.toList()));
        }
        if (CollectionUtil.isNotEmpty(allOfficeScores)) {
            allScoreIds.addAll(allOfficeScores.stream().map(BaseOfficeScore::getId).collect(Collectors.toList()));
        }
        if (CollectionUtil.isNotEmpty(allAssessScores)) {
            allScoreIds.addAll(allAssessScores.stream().map(BaseAssessScore::getId).collect(Collectors.toList()));
        }
        List<EvaluationScoreDetail> allScoreDetails = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(allScoreIds)) {
            allScoreDetails = scoreDetailService.list(new LambdaQueryWrapper<EvaluationScoreDetail>().in(EvaluationScoreDetail::getBaseScoreId, allScoreIds));
        }
        List<BaseBusinessAudit> allBaseBusinessAudits = baseBusinessAuditService.list(new LambdaQueryWrapper<BaseBusinessAudit>().eq(BaseBusinessAudit::getFillingYear, dto.getYear())
        );
        List<MainAudit> allMainAudits = mainAuditService.list();
        List<MainBusiness> allMainBusinesses = mainBusinessService.list();
        List<MainAnnual> allMainAnnuals = mainAnnualService.list();
        List<AuditAssets> allAuditAssets = auditAssetsService.list();

        //todo 获取13家
        List<Org> orgList = getOrgList(dto.getOrgId());
        List<Org> allOrg = orgService.getAllOrg();
        for (Org org : orgList) {
            String orgId = org.getId();
            String orgName = org.getName();
            //子企业ids
            List<String> orgIds = allOrg.stream().filter(m -> m.getOrgCode().contains(org.getOrgCode())).map(Org::getId).collect(Collectors.toList());

            List<BaseEvaluationScore> evaluationScores = new ArrayList<>();
            List<BaseOfficeScore> officeScores = new ArrayList<>();
            List<BaseAssessScore> assessScores = new ArrayList<>();
            List<EvaluationScoreDetail> scoreDetails = new ArrayList<>();
            List<Long> scoreIds = new ArrayList<>();
            if (CollectionUtil.isNotEmpty(allEvaluationScores)) {
                evaluationScores = allEvaluationScores.stream().filter(m -> orgIds.contains(m.getFillingUnitId())).collect(Collectors.toList());
                if (CollectionUtil.isNotEmpty(evaluationScores)) {
                    scoreIds.addAll(evaluationScores.stream().map(BaseEvaluationScore::getId).collect(Collectors.toList()));
                }
            }
            if (CollectionUtil.isNotEmpty(allOfficeScores)) {
                officeScores = allOfficeScores.stream().filter(m -> orgIds.contains(m.getFillingUnitId())).collect(Collectors.toList());
                if (CollectionUtil.isNotEmpty(officeScores)) {
                    scoreIds.addAll(officeScores.stream().map(BaseOfficeScore::getId).collect(Collectors.toList()));
                }
            }
            if (CollectionUtil.isNotEmpty(allAssessScores)) {
                assessScores = allAssessScores.stream().filter(m -> orgIds.contains(m.getFillingUnitId())).collect(Collectors.toList());
                if (CollectionUtil.isNotEmpty(assessScores)) {
                    scoreIds.addAll(assessScores.stream().map(BaseAssessScore::getId).collect(Collectors.toList()));
                }
            }
            if (CollectionUtil.isNotEmpty(scoreIds)) {
                scoreDetails = allScoreDetails.stream().filter(m -> scoreIds.contains(m.getBaseScoreId())).collect(Collectors.toList());
            }
            if (CollectionUtil.isNotEmpty(allAppraisalVOS)) {
                List<AppraisalIndexVO> indexVOS = allAppraisalVOS.get(0).getChildren();
                for (AppraisalIndexVO indexVO : indexVOS) {
                    String firstIndex = indexVO.getName();
                    Integer sort = indexVO.getSort();
                    List<AppraisalIndexVO> secondaryChild = indexVO.getChildren();
                    for (AppraisalIndexVO secondaryIndex : secondaryChild) {
                        //todo 二级指标暂存，批量修改权重
                        List<PerformanceReportVO> secondaryReport = new ArrayList<>();
                        BigDecimal secondaryDecimal = BigDecimal.ZERO;
                        String secondaryName = secondaryIndex.getName();
                        List<AppraisalIndexVO> threeChild = secondaryIndex.getChildren();
                        for (AppraisalIndexVO threeIndex : threeChild) {
                            String threeName = threeIndex.getName();
                            //评分办法
                            if (CollectionUtil.isNotEmpty(scoreMethods)) {
                                List<PerformanceScoreMethod> methods = scoreMethods.stream().filter(m -> threeIndex.getId().equals(m.getBaseBusinessIndicatorsId())).collect(Collectors.toList());
                                if (CollectionUtil.isNotEmpty(methods)) {
                                    for (PerformanceScoreMethod scoreMethod : methods) {
                                        PerformanceReportVO reportVO = new PerformanceReportVO();
                                        reportVO.setSort(sort);
                                        reportVO.setOrgId(orgId);
                                        reportVO.setOrgName(orgName);
                                        reportVO.setFirstIndex(firstIndex);
                                        reportVO.setThreeIndex(threeName);
                                        BigDecimal weight = scoreMethod.getWeight();
                                        if (Optional.ofNullable(weight).isPresent()) {
                                            weight = weight.setScale(2, RoundingMode.HALF_UP);
                                            secondaryDecimal = secondaryDecimal.add(weight);
                                            reportVO.setWeight(weight.toPlainString() + "%");
                                        } else {
                                            reportVO.setWeight("100%");
                                        }
                                        reportVO.setContent(scoreMethod.getCheckItem());
                                        reportVO.setGroupFlag(firstIndex + "_" + secondaryName);
                                        setScore(reportVO, weight, scoreMethod.getId(), evaluationScores, officeScores, assessScores, scoreDetails);
                                        secondaryReport.add(reportVO);
                                    }
                                }
                            }
                        }
                        //权重修改
                        for (PerformanceReportVO reportVO : secondaryReport) {
                            reportVO.setSecondaryIndex(secondaryName + "(" + secondaryDecimal.setScale(2, RoundingMode.HALF_UP).toPlainString() + "%)");
                        }
                        performanceReportVOS.addAll(secondaryReport);
                    }
                }
            }
            //添加责任担当
            if (CollectionUtil.isNotEmpty(allBaseBusinessAudits)) {
                Optional<BaseBusinessAudit> auditOptional = allBaseBusinessAudits.stream().filter(m -> orgIds.contains(m.getFillingUnitId())).findFirst();
                if (auditOptional.isPresent()) {
                    BaseBusinessAudit audit = auditOptional.get();
                    //主责主业
                    List<MainAudit> mainAudits = allMainAudits.stream().filter(m -> audit.getId().equals(m.getBaseBusinessAuditId())).collect(Collectors.toList());
                    //企业自评
                    List<AuditAssets> auditAssets = allAuditAssets.stream().filter(m -> audit.getId().equals(m.getBaseBusinessAuditId())).collect(Collectors.toList());

                    if (CollectionUtil.isNotEmpty(mainAudits)) {
                        List<Long> mainAuditIds = mainAudits.stream().map(MainAudit::getMainBusinessId).collect(Collectors.toList());
                        List<MainBusiness> mainBusinesses = allMainBusinesses.stream().filter(m -> mainAuditIds.contains(m.getId())).collect(Collectors.toList());
                        List<MainAnnual> mainAnnuals = allMainAnnuals.stream().filter(m -> mainAuditIds.contains(m.getMainBusinessId())).collect(Collectors.toList());
                        int i = 1;
                        BigDecimal weight = new BigDecimal("20").divide(new BigDecimal(mainAudits.size()), 2, RoundingMode.HALF_UP);
                        for (MainAudit mainAudit : mainAudits) {
                            PerformanceReportVO reportVO = new PerformanceReportVO();
                            reportVO.setSort(2);
                            reportVO.setOrgId(orgId);
                            reportVO.setOrgName(orgName);
                            reportVO.setFirstIndex("责任担当");
                            reportVO.setGroupFlag("责任担当_主责主业" + i);

                            //获取指标
                            Optional<MainBusiness> optional = mainBusinesses.stream().filter(m -> mainAudit.getMainBusinessId().equals(m.getId())).findFirst();
                            if (optional.isPresent()) {
                                MainBusiness mainBusiness = optional.get();
                                reportVO.setSecondaryIndex("主责主业" + i + ":" + mainBusiness.getEvaluation());
                            }
                            i++;
                            reportVO.setWeight(weight.toPlainString() + "%");
                            Optional<MainAnnual> annualOptional = mainAnnuals.stream().filter(m -> mainAudit.getMainBusinessId().equals(m.getMainBusinessId())).findFirst();
                            if (annualOptional.isPresent()) {
                                MainAnnual mainAnnual = annualOptional.get();
                                BigDecimal selfScore = mainAnnual.getSelfScore() != null ? mainAnnual.getSelfScore().setScale(2, RoundingMode.HALF_UP) : BigDecimal.ZERO;
                                reportVO.setEnterpriseScore(selfScore);
                                reportVO.setEnterpriseResult(selfScore.multiply(weight).divide(new BigDecimal(100), 2, RoundingMode.HALF_UP));
                            }
                            BigDecimal auditScore = mainAudit.getAuditScore() != null ? mainAudit.getAuditScore().setScale(2, RoundingMode.HALF_UP) : BigDecimal.ZERO;
                            reportVO.setAssetScore(auditScore);
                            reportVO.setAssetResult(auditScore.multiply(weight).divide(new BigDecimal(100), 2, RoundingMode.HALF_UP));
                            performanceReportVOS.add(reportVO);
                        }
                    }
                    if (CollectionUtil.isNotEmpty(auditAssets)) {
                        BigDecimal weight = new BigDecimal("20");
                        //有效投资
                        Optional<AuditAssets> first = auditAssets.stream().filter(m -> Optional.ofNullable(m.getType()).isPresent() && m.getType() == 1).findFirst();
                        //重大项目
                        Optional<AuditAssets> project = auditAssets.stream().filter(m -> Optional.ofNullable(m.getType()).isPresent() && m.getType() == 2).findFirst();
                        if (first.isPresent()) {
                            if (project.isPresent()) {
                                weight = new BigDecimal("10");
                            }
                            AuditAssets assets = first.get();
                            PerformanceReportVO reportVO = new PerformanceReportVO();
                            reportVO.setSort(2);
                            reportVO.setOrgId(orgId);
                            reportVO.setOrgName(orgName);
                            reportVO.setFirstIndex("责任担当");
                            reportVO.setSecondaryIndex("有效投资(" + weight + "%)");
                            reportVO.setWeight(weight.toPlainString() + "%");
                            reportVO.setGroupFlag("责任担当_有效投资");
                            BigDecimal selfScore = assets.getSelfScore() != null ? assets.getSelfScore().setScale(2, RoundingMode.HALF_UP) : BigDecimal.ZERO;
                            reportVO.setEnterpriseScore(selfScore);
                            reportVO.setEnterpriseResult(selfScore.multiply(weight).divide(new BigDecimal(100), 2, RoundingMode.HALF_UP));

                            BigDecimal auditSelfScore = assets.getAuditSelfScore() != null ? assets.getAuditSelfScore().setScale(2, RoundingMode.HALF_UP) : BigDecimal.ZERO;
                            reportVO.setAssetScore(auditSelfScore);
                            reportVO.setAssetResult(auditSelfScore.multiply(weight).divide(new BigDecimal(100), 2, RoundingMode.HALF_UP));
                            performanceReportVOS.add(reportVO);
                        }

                        if (project.isPresent()) {
                            if (!first.isPresent()) {
                                weight = new BigDecimal("20");
                            }
                            AuditAssets assets = project.get();
                            PerformanceReportVO reportVO = new PerformanceReportVO();
                            reportVO.setSort(2);
                            reportVO.setOrgId(orgId);
                            reportVO.setOrgName(orgName);
                            reportVO.setFirstIndex("责任担当");
                            reportVO.setSecondaryIndex("重大项目(" + weight + "%)");
                            reportVO.setGroupFlag("责任担当_重大项目");
                            reportVO.setWeight(weight.toPlainString() + "%");
                            BigDecimal selfScore = assets.getSelfScore() != null ? assets.getSelfScore().setScale(2, RoundingMode.HALF_UP) : BigDecimal.ZERO;
                            reportVO.setEnterpriseScore(selfScore);
                            reportVO.setEnterpriseResult(selfScore.multiply(weight).divide(new BigDecimal(100), 2, RoundingMode.HALF_UP));

                            BigDecimal auditSelfScore = assets.getAuditSelfScore() != null ? assets.getAuditSelfScore().setScale(2, RoundingMode.HALF_UP) : BigDecimal.ZERO;
                            reportVO.setAssetScore(auditSelfScore);
                            reportVO.setAssetResult(auditSelfScore.multiply(weight).divide(new BigDecimal(100), 2, RoundingMode.HALF_UP));
                            performanceReportVOS.add(reportVO);
                        }
                    }
                }
            }
        }
        if (CollectionUtil.isNotEmpty(performanceReportVOS)) {
            performanceReportVOS = performanceReportVOS.stream().sorted(Comparator.comparing(PerformanceReportVO::getSort)).collect(Collectors.toList());
        }
        return performanceReportVOS;
    }

    @Override
    public ReportReturnVO getBusinessMonthReport(PerformanceReportDTO dto) {
        List<BusinessMonthReportVO> businessMonthReportVOS = new ArrayList<>();

        //指标
        List<AppraisalIndex> appraisalIndices = appraisalIndexService.list(new LambdaQueryWrapper<AppraisalIndex>().eq(AppraisalIndex::getTableCode, "001").eq(AppraisalIndex::getType, 1).eq(AppraisalIndex::getFlag, 0));
        //基础-财务绩效
        List<BusinessIndicators> businessIndicators = businessIndicatorsService.list();

        //经营绩效月度表
        List<BaseBusinessProgress> baseBusinessProgresses = baseBusinessProgressService.list(new LambdaQueryWrapper<BaseBusinessProgress>().like(BaseBusinessProgress::getFillingMonth, dto.getYear())
                .eq(BaseBusinessProgress::getFillingUnitId, dto.getOrgId()));
        //月度表-财务绩效
        List<BusinessProgress> businessProgresses = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(baseBusinessProgresses)) {
            List<Long> progressIds = baseBusinessProgresses.stream().map(BaseBusinessProgress::getId).collect(Collectors.toList());
            businessProgresses = businessProgressService.list(new LambdaQueryWrapper<BusinessProgress>().in(BusinessProgress::getBaseBusinessProgressId, progressIds));
        }

        List<BusinessIndicatorsReportVO> businessIndicatorsReports = getBusinessIndicatorsReport(dto);
        //经营业绩 小计得分
        BusinessMonthReportVO subReport = businessToMonth(getBusinessCountValue(businessIndicatorsReports).get(0));
        if (CollectionUtil.isNotEmpty(businessIndicatorsReports)) {
            for (BusinessIndicatorsReportVO businessIndicatorsReportVO : businessIndicatorsReports) {
                BusinessMonthReportVO businessMonthReportVO = businessToMonth(businessIndicatorsReportVO);
                setMonthValue(businessMonthReportVO, baseBusinessProgresses, businessProgresses, appraisalIndices, businessIndicators);
                businessMonthReportVOS.add(businessMonthReportVO);
            }
        }
        businessMonthReportVOS.add(subReport);
        //报表头部
        ReportVO reportVO = new ReportVO();
        List<HeaderVO> indexs = new ArrayList<>();
        indexs.add(HeaderVO.builder().key("firstIndex").title("一级指标").build());
        indexs.add(HeaderVO.builder().key("secondaryIndex").title("二级指标").build());
        indexs.add(HeaderVO.builder().key("threeIndex").title("三级指标").build());

        List<HeaderVO> enterprises = new ArrayList<>();
        enterprises.add(HeaderVO.builder().key("enterpriseScore").title("完成值").build());
        enterprises.add(HeaderVO.builder().key("enterpriseResult").title("折合得分").build());

        List<HeaderVO> offices = new ArrayList<>();
        offices.add(HeaderVO.builder().key("officeScore").title("完成值").build());
        offices.add(HeaderVO.builder().key("officeResult").title("折合得分").build());

        List<HeaderVO> assets = new ArrayList<>();
        assets.add(HeaderVO.builder().key("assetScore").title("完成值").build());
        assets.add(HeaderVO.builder().key("assetResult").title("折合得分").build());

        reportVO
                .addHeader(HeaderVO.builder().title("指标名称").child(indexs).build())
                .addHeader(HeaderVO.builder().title("指标权重").key("weight").build())
                .addHeader(HeaderVO.builder().title("基准值").key("baseline").build())
                .addHeader(HeaderVO.builder().title("基本目标值").key("basicTarget").build())
                .addHeader(HeaderVO.builder().title("激励目标值").key("target").build())

                .addHeader(HeaderVO.builder().title("1月").key("january").build())
                .addHeader(HeaderVO.builder().title("2月").key("february").build())
                .addHeader(HeaderVO.builder().title("3月").key("march").build())
                .addHeader(HeaderVO.builder().title("4月").key("april").build())
                .addHeader(HeaderVO.builder().title("5月").key("may").build())
                .addHeader(HeaderVO.builder().title("6月").key("june").build())
                .addHeader(HeaderVO.builder().title("7月").key("july").build())
                .addHeader(HeaderVO.builder().title("8月").key("august").build())
                .addHeader(HeaderVO.builder().title("9月").key("september").build())
                .addHeader(HeaderVO.builder().title("10月").key("october").build())
                .addHeader(HeaderVO.builder().title("11月").key("november").build())
                .addHeader(HeaderVO.builder().title("12月").key("december").build())

                .addHeader(HeaderVO.builder().title("企业年度上报").child(enterprises).build())
                .addHeader(HeaderVO.builder().title("审计认定").child(offices).build())
                .addHeader(HeaderVO.builder().title("国资审核").child(assets).build())
        ;
        reportVO.setData(businessMonthReportVOS);
        ReportReturnVO reportReturnVO = new ReportReturnVO();
        reportReturnVO.setGrids(reportVO);

        return reportReturnVO;
    }


    /**
     * @Author: Take-off
     * @Description: //TODO 放入当月执行数
     * @Date: 9:45 AM 2023/2/7
     * @Param: [businessMonthReportVO, baseBusinessProgresses, businessProgresses, appraisalIndices, businessIndicators]
     * @return: void
     **/
    private void setMonthValue(BusinessMonthReportVO businessMonthReportVO, List<BaseBusinessProgress> baseBusinessProgresses,
                               List<BusinessProgress> businessProgresses, List<AppraisalIndex> appraisalIndices, List<BusinessIndicators> businessIndicators) {
        //指标所在id
        List<AppraisalIndex> appraisalIndexList = appraisalIndices.stream().filter(m -> businessMonthReportVO.getThreeIndex().equals(m.getName())).collect(Collectors.toList());
        if (CollectionUtil.isNotEmpty(appraisalIndexList)) {
            AppraisalIndex appraisalIndex = appraisalIndexList.get(0);
            for (BaseBusinessProgress baseBusinessProgress : baseBusinessProgresses) {
                String month = baseBusinessProgress.getFillingMonth().split("-")[1];
                switch (month) {
                    case "01":
                        businessMonthReportVO.setJanuary(getMonthValue(appraisalIndex, baseBusinessProgress, businessProgresses, businessIndicators));
                        break;
                    case "02":
                        businessMonthReportVO.setFebruary(getMonthValue(appraisalIndex, baseBusinessProgress, businessProgresses, businessIndicators));
                        break;
                    case "03":
                        businessMonthReportVO.setMarch(getMonthValue(appraisalIndex, baseBusinessProgress, businessProgresses, businessIndicators));
                        break;
                    case "04":
                        businessMonthReportVO.setApril(getMonthValue(appraisalIndex, baseBusinessProgress, businessProgresses, businessIndicators));
                        break;
                    case "05":
                        businessMonthReportVO.setMay(getMonthValue(appraisalIndex, baseBusinessProgress, businessProgresses, businessIndicators));
                        break;
                    case "06":
                        businessMonthReportVO.setJune(getMonthValue(appraisalIndex, baseBusinessProgress, businessProgresses, businessIndicators));
                        break;
                    case "07":
                        businessMonthReportVO.setJuly(getMonthValue(appraisalIndex, baseBusinessProgress, businessProgresses, businessIndicators));
                        break;
                    case "08":
                        businessMonthReportVO.setAugust(getMonthValue(appraisalIndex, baseBusinessProgress, businessProgresses, businessIndicators));
                        break;
                    case "09":
                        businessMonthReportVO.setSeptember(getMonthValue(appraisalIndex, baseBusinessProgress, businessProgresses, businessIndicators));
                        break;
                    case "10":
                        businessMonthReportVO.setOctober(getMonthValue(appraisalIndex, baseBusinessProgress, businessProgresses, businessIndicators));
                        break;
                    case "11":
                        businessMonthReportVO.setNovember(getMonthValue(appraisalIndex, baseBusinessProgress, businessProgresses, businessIndicators));
                        break;
                    case "12":
                        businessMonthReportVO.setDecember(getMonthValue(appraisalIndex, baseBusinessProgress, businessProgresses, businessIndicators));
                        break;
                    default:
                        break;
                }
            }
        }
    }

    /**
     * @Author: Take-off
     * @Description: //TODO 获取当月执行数
     * @Date: 9:39 AM 2023/2/7
     * @Param: [month, appraisalIndex, baseBusinessProgress, businessProgresses, businessIndicators]
     * @return: java.math.BigDecimal
     **/
    private BigDecimal getMonthValue(AppraisalIndex appraisalIndex, BaseBusinessProgress baseBusinessProgress,
                                     List<BusinessProgress> businessProgresses, List<BusinessIndicators> businessIndicators) {
        BigDecimal value = BigDecimal.ZERO;
        List<BusinessIndicators> businessIndicatorsList = businessIndicators.stream().filter(m -> appraisalIndex.getId().equals(m.getIndicatorsCategoryId())).collect(Collectors.toList());
        if (CollectionUtil.isNotEmpty(businessIndicatorsList)) {
            List<Long> indicatorIds = businessIndicatorsList.stream().map(BusinessIndicators::getId).collect(Collectors.toList());
            List<BusinessProgress> progresses = businessProgresses.stream().filter(progress -> baseBusinessProgress.getId().equals(progress.getBaseBusinessProgressId()) && indicatorIds.contains(progress.getBusinessIndicatorsId())).collect(Collectors.toList());
            if (CollectionUtil.isNotEmpty(progresses)) {
                BusinessProgress businessProgress = progresses.get(0);
                if (Optional.ofNullable(businessProgress.getPerformFill()).isPresent()) {
                    value = businessProgress.getPerformFill();
                } else {
                    value = businessProgress.getPerformCalculate();
                }

            }
        }
        return value;
    }


    /**
     * @Author: Take-off
     * @Description: //TODO 汇总得分 转变为 经营业绩
     * @Date: 8:14 PM 2023/2/6
     * @Param: [businessIndicatorsReportVO]
     * @return: com.plian.system.vo.pf.BusinessMonthReportVO
     **/
    private BusinessMonthReportVO businessToMonth(BusinessIndicatorsReportVO businessIndicatorsReportVO) {
        BusinessMonthReportVO businessMonthReportVO = new BusinessMonthReportVO();

        businessMonthReportVO.setFirstIndex(businessIndicatorsReportVO.getFirstIndex());
        businessMonthReportVO.setSecondaryIndex(businessIndicatorsReportVO.getSecondaryIndex());
        businessMonthReportVO.setThreeIndex(businessIndicatorsReportVO.getThreeIndex());
        businessMonthReportVO.setContent(businessIndicatorsReportVO.getContent());
        businessMonthReportVO.setWeight(businessIndicatorsReportVO.getWeight());
        businessMonthReportVO.setEnterpriseScore(businessIndicatorsReportVO.getEnterpriseScore());
        businessMonthReportVO.setEnterpriseResult(businessIndicatorsReportVO.getEnterpriseResult());
        businessMonthReportVO.setOfficeScore(businessIndicatorsReportVO.getOfficeScore());
        businessMonthReportVO.setOfficeResult(businessIndicatorsReportVO.getOfficeResult());
        businessMonthReportVO.setAssetScore(businessIndicatorsReportVO.getAssetScore());
        businessMonthReportVO.setAssetResult(businessIndicatorsReportVO.getAssetResult());
        businessMonthReportVO.setEnterpriseItem(businessIndicatorsReportVO.getEnterpriseItem());
        businessMonthReportVO.setOfficeItem(businessIndicatorsReportVO.getOfficeItem());
        businessMonthReportVO.setAssetItem(businessIndicatorsReportVO.getAssetItem());

        businessMonthReportVO.setBaseline(businessIndicatorsReportVO.getBaseline());
        businessMonthReportVO.setTarget(businessIndicatorsReportVO.getTarget());
        businessMonthReportVO.setBasicTarget(businessIndicatorsReportVO.getBasicTarget());

        return businessMonthReportVO;
    }


    /**
     * @Author: Take-off
     * @Description: //TODO 获取经营业绩 考核得分
     * @Date: 3:45 PM 2023/2/5
     * @Param: [dto]
     * @return: java.util.List<com.plian.system.vo.pf.BusinessIndicatorsReportVO>
     **/
    private List<BusinessIndicatorsReportVO> getBusinessIndicatorsReport(PerformanceReportDTO dto) {
        List<BusinessIndicatorsReportVO> businessIndicatorsReportVOS = new ArrayList<>();
        //todo 获取13家
        List<Org> orgList = getOrgList(dto.getOrgId());
        List<Org> allOrg = orgService.getAllOrg();
        //指标
        List<AppraisalIndex> appraisalIndices = appraisalIndexService.list(new LambdaQueryWrapper<AppraisalIndex>().eq(AppraisalIndex::getTableCode, "001").eq(AppraisalIndex::getType, 1).eq(AppraisalIndex::getFlag, 0));
        //经营绩效基础表
        List<BaseBusinessIndicators> allBaseBusinessIndicators = baseBusinessIndicatorsService.list(new LambdaQueryWrapper<BaseBusinessIndicators>().eq(BaseBusinessIndicators::getFillingYear, dto.getYear())
                .eq(BaseBusinessIndicators::getIsLatest, 2));
        //财务绩效
        List<BusinessIndicators> allBusinessIndicators = new ArrayList<>();
        //任务绩效
        List<TaskIndicators> allTaskIndicators = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(allBaseBusinessIndicators)) {
            allBusinessIndicators = businessIndicatorsService.list(new LambdaQueryWrapper<BusinessIndicators>().isNotNull(BusinessIndicators::getIndicatorsCategoryId));
            allTaskIndicators = taskIndicatorsService.list(new LambdaQueryWrapper<TaskIndicators>().isNotNull(TaskIndicators::getIndicatorsCategoryId));
        }
        //国资审核
        List<BaseBusinessAudit> allBaseBusinessAudits = baseBusinessAuditService.list(new LambdaQueryWrapper<BaseBusinessAudit>().eq(BaseBusinessAudit::getFillingYear, dto.getYear())
        );
        //国资-财务子项
        List<BusinessAudit> allBusinessAudits = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(allBaseBusinessAudits)) {
            allBusinessAudits = businessAuditService.list();
        }
//        } else {
//            businessAudits = setBusinessAudits(dto, appraisalIndices, businessIndicators);
//        }
        //年度
        List<BaseBusinessAnnual> allBaseBusinessAnnuals = baseBusinessAnnualService.list(new LambdaQueryWrapper<BaseBusinessAnnual>().eq(BaseBusinessAnnual::getFillingYear, dto.getYear())
        );
        List<TaskAnnual> allTaskAnnuals = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(allBaseBusinessAnnuals)) {
            allTaskAnnuals = taskAnnualService.list();
        }


        for (Org org : orgList) {
            String orgId = org.getId();
            String unitId = org.getId();
            String orgName = org.getName();
            //子企业ids
            List<String> orgIds = allOrg.stream().filter(m -> m.getOrgCode().contains(org.getOrgCode())).map(Org::getId).collect(Collectors.toList());

            Long baseId = null;
            List<BusinessIndicators> businessIndicators = new ArrayList<>();
            List<TaskIndicators> taskIndicators = new ArrayList<>();
            List<BusinessAudit> businessAudits = new ArrayList<>();
            List<TaskAnnual> taskAnnuals = new ArrayList<>();
            if (CollectionUtil.isNotEmpty(allBaseBusinessIndicators)) {
                Optional<BaseBusinessIndicators> indicatorsOptional = allBaseBusinessIndicators.stream().filter(m -> orgIds.contains(m.getFillingUnitId())).findFirst();
                if (indicatorsOptional.isPresent()) {
                    BaseBusinessIndicators baseBusinessIndicators = indicatorsOptional.get();
                    unitId = baseBusinessIndicators.getFillingUnitId();
                    baseId = baseBusinessIndicators.getId();
                    if (CollectionUtil.isNotEmpty(allBusinessIndicators)) {
                        businessIndicators = allBusinessIndicators.stream().filter(m -> baseBusinessIndicators.getId().equals(m.getBaseBusinessIndicatorsId())).collect(Collectors.toList());
                    }
                    if (CollectionUtil.isNotEmpty(allTaskIndicators)) {
                        taskIndicators = allTaskIndicators.stream().filter(m -> baseBusinessIndicators.getId().equals(m.getBaseBusinessIndicatorsId())).collect(Collectors.toList());
                    }
                }
            }
            if (CollectionUtil.isNotEmpty(allBaseBusinessAudits)) {
                Optional<BaseBusinessAudit> auditOptional = allBaseBusinessAudits.stream().filter(m -> orgIds.contains(m.getFillingUnitId())).findFirst();
                if (auditOptional.isPresent() && CollectionUtil.isNotEmpty(allBusinessAudits)) {
                    businessAudits = allBusinessAudits.stream().filter(m -> auditOptional.get().getId().equals(m.getBaseBusinessAuditId())).collect(Collectors.toList());
                }
            }
            if (CollectionUtil.isNotEmpty(allBaseBusinessAnnuals)) {
                Optional<BaseBusinessAnnual> annualOptional = allBaseBusinessAnnuals.stream().filter(m -> orgIds.contains(m.getFillingUnitId())).findFirst();
                if (annualOptional.isPresent() && CollectionUtil.isNotEmpty(allTaskAnnuals)) {
                    taskAnnuals = allTaskAnnuals.stream().filter(m -> annualOptional.get().getId().equals(m.getBaseBusinessAnnualId())).collect(Collectors.toList());
                }
            }
            //财务绩效
            if (CollectionUtil.isNotEmpty(businessIndicators)) {
                for (BusinessIndicators businessIndicator : businessIndicators) {
                    BusinessIndicatorsReportVO reportVO = new BusinessIndicatorsReportVO();
                    reportVO.setOrgId(orgId);
                    reportVO.setOrgName(orgName);
                    AppraisalIndex appraisalIndex = appraisalIndices.stream().filter(m -> businessIndicator.getIndicatorsCategoryId().equals(m.getId())).findFirst().get();
                    Long indexId = appraisalIndex.getId();
                    reportVO.setThreeIndex(appraisalIndex.getName());
                    reportVO.setFirstIndex("经营业绩");
                    reportVO.setSecondaryIndex("财务绩效");
                    reportVO.setThreeIndex(appraisalIndex.getName());
                    reportVO.setSort(appraisalIndex.getSort());
                    List<BusinessIndicators> businessIndicatorsList = businessIndicators.stream().filter(m -> indexId.equals(m.getIndicatorsCategoryId())).collect(Collectors.toList());
                    //绩效基础值
                    BigDecimal weight = BigDecimal.ZERO;
                    if (CollectionUtil.isNotEmpty(businessIndicatorsList)) {
//                    BusinessIndicators businessIndicator = businessIndicatorsList.get(0);
                        Long businessIndicatorId = businessIndicator.getId();
                        weight = businessIndicator.getIndicatorsWeight() != null ? businessIndicator.getIndicatorsWeight().setScale(2, RoundingMode.HALF_UP) : BigDecimal.ZERO;
                        reportVO.setWeight(weight + "%");
                        reportVO.setBaseline(businessIndicator.getBaseline());
                        reportVO.setTarget(businessIndicator.getTarget());
                        reportVO.setBasicTarget(businessIndicator.getBasicTarget());
                        reportVO.setGroupFlag("财务绩效_" + appraisalIndex.getName());

                        //绩效分数/计算值
                        if (CollectionUtil.isNotEmpty(businessAudits)) {
                            List<BusinessAudit> businessAuditList = businessAudits.stream().filter(m -> businessIndicatorId.equals(m.getBusinessIndicatorsId())).collect(Collectors.toList());
                            if (CollectionUtil.isNotEmpty(businessAuditList)) {
                                BusinessAudit businessAudit = businessAuditList.get(0);
                                reportVO.setEnterpriseScore(businessAudit.getAnnualSubmit());
                                reportVO.setOfficeScore(businessAudit.getAuditFirmly());
                                reportVO.setAssetScore(businessAudit.getAuditSum());

                                try {
                                    //后台行业配置
                                    BpaIndustryStandardConfig unitIndustryStandardConfig = operatingPerformanceService.getUnitIndustryStandardConfig(dto.getYear(), unitId);
                                    if (Objects.isNull(unitIndustryStandardConfig)) {
                                        log.info("该单位未配置行业类型！");
                                        continue;
                                    }
                                    //后台查询配置信息
                                    List<BusinessAssessment> businessAssessmentList = businessAssessmentService.list(Wrappers.<BusinessAssessment>lambdaQuery()
                                            .eq(BusinessAssessment::getEnabled, 1)
                                            .eq(BusinessAssessment::getIsYear, dto.getYear())
                                            .eq(BusinessAssessment::getIndicatorsId, indexId));

                                    if (CollectionUtil.isEmpty(businessAssessmentList)) {
                                        log.info("该年份未配置【" + appraisalIndex.getName() + "】财务绩效指标配置！");
                                        continue;
                                    }

                                    List<Long> performanceIds = businessAssessmentList.stream().map(BusinessAssessment::getId).collect(Collectors.toList());
                                    //档位配置信息
                                    List<BusinessAssessmentGear> gearList = businessAssessmentGearService.list(Wrappers.<BusinessAssessmentGear>lambdaQuery().in(BusinessAssessmentGear::getBusinessAssessmentId, performanceIds));
                                    //计分配置情况
                                    List<BusinessAssessmentScore> scoreList = businessAssessmentScoreService.list(Wrappers.<BusinessAssessmentScore>lambdaQuery().in(BusinessAssessmentScore::getBusinessAssessmentId, performanceIds));

                                    /**
                                     * 分项得分
                                     */
                                    //企业
                                    BigDecimal annualSubmit = businessAudit.getAnnualSubmit();
                                    BigDecimal enterpriseItem = BigDecimal.ZERO;
                                    if (annualSubmit != null && annualSubmit.compareTo(BigDecimal.ZERO) != 0) {
                                        BigDecimal currEnterpriseItem = operatingPerformanceService.newMakeSureScores(unitId, dto.getYear(), businessAudit, annualSubmit, gearList,
                                                scoreList, businessAssessmentList, unitIndustryStandardConfig, appraisalIndex);
                                        if (currEnterpriseItem != null) {
                                            enterpriseItem = currEnterpriseItem;
                                            reportVO.setEnterpriseItem(enterpriseItem);
                                        }
                                    }

                                    //责任处室
                                    BigDecimal officeItem = BigDecimal.ZERO;
                                    BigDecimal auditFirmly = businessAudit.getAuditFirmly();
                                    if (auditFirmly != null && auditFirmly.compareTo(BigDecimal.ZERO) != 0) {
                                        BigDecimal currOfficeItem = operatingPerformanceService.newMakeSureScores(unitId, dto.getYear(), businessAudit, auditFirmly, gearList,
                                                scoreList, businessAssessmentList, unitIndustryStandardConfig, appraisalIndex);
                                        if (currOfficeItem != null) {
                                            officeItem = currOfficeItem;
                                            reportVO.setOfficeItem(officeItem);
                                        }
                                    }

                                    //国资
                                    BigDecimal assetItem = BigDecimal.ZERO;
                                    BigDecimal auditSum = businessAudit.getAuditSum();
                                    if (auditSum != null && auditSum.compareTo(BigDecimal.ZERO) != 0) {
                                        BigDecimal currAssetItem = operatingPerformanceService.newMakeSureScores(unitId, dto.getYear(), businessAudit, auditSum, gearList,
                                                scoreList, businessAssessmentList, unitIndustryStandardConfig, appraisalIndex);
                                        if (currAssetItem != null) {
                                            assetItem = currAssetItem;
                                            reportVO.setAssetItem(assetItem);
                                        }
                                    }


                                    /**
                                     * 折合得分 = = 分项*权重
                                     */
                                    BigDecimal indicatorsWeight = businessAudit.getIndicatorsWeight();
                                    if (indicatorsWeight != null) {
                                        BigDecimal currentIndicatorsWeight = indicatorsWeight.divide(new BigDecimal(100));
                                        reportVO.setEnterpriseResult(enterpriseItem.multiply(currentIndicatorsWeight).setScale(4, RoundingMode.HALF_UP));
                                        reportVO.setOfficeResult(officeItem.multiply(currentIndicatorsWeight).setScale(4, RoundingMode.HALF_UP));
                                        reportVO.setAssetResult(assetItem.multiply(currentIndicatorsWeight).setScale(4, RoundingMode.HALF_UP));
                                    }
                                } catch (Exception e) {
                                    log.error("error:折合得分计算出错{}", e);
                                }
                            }
                        }
                    }
                    businessIndicatorsReportVOS.add(reportVO);
                }
            }
            //任务绩效
            if (CollectionUtil.isNotEmpty(taskIndicators)) {
                int i = 1;
                for (TaskIndicators taskIndicator : taskIndicators) {
                    BusinessIndicatorsReportVO reportVO = new BusinessIndicatorsReportVO();
                    reportVO.setOrgId(orgId);
                    reportVO.setOrgName(orgName);
                    reportVO.setThreeIndex(taskIndicator.getContent());
                    reportVO.setSecondaryIndex("任务绩效");
                    reportVO.setFirstIndex("经营业绩");
                    reportVO.setGroupFlag("任务绩效_" + i++);
                    reportVO.setSort(99 + i);
                    //年度
                    if (CollectionUtil.isNotEmpty(taskAnnuals)) {
                        Optional<TaskAnnual> first = taskAnnuals.stream().filter(m -> taskIndicator.getId().equals(m.getTaskIndicatorsId())).findFirst();
                        if (first.isPresent()) {
                            TaskAnnual taskAnnual = first.get();
                            reportVO.setEnterpriseResult(taskAnnual.getSelfScoring());
                        }
                    }
                    businessIndicatorsReportVOS.add(reportVO);
                }
            }
        }
        if (CollectionUtil.isNotEmpty(businessIndicatorsReportVOS)) {
            businessIndicatorsReportVOS = businessIndicatorsReportVOS.stream().sorted(Comparator.comparing(BusinessIndicatorsReportVO::getSort)).collect(Collectors.toList());
        }

        return businessIndicatorsReportVOS;
    }

    /**
     * @Author: Take-off
     * @Description: //TODO 获取年度审核和审定认定
     * @Date: 3:26 PM 2023/2/5
     * @Param: [dto, appraisalIndices, businessIndicators]
     * @return: java.util.List<com.plian.system.entity.pf.BusinessAudit>
     **/
    private List<BusinessAudit> setBusinessAudits(PerformanceReportDTO dto, List<AppraisalIndex> appraisalIndices, List<BusinessIndicators> businessIndicators) {
        List<BusinessAudit> businessAuditVOS = new ArrayList<>();

        List<IndicatorsValue> indicatorsValues = indicatorsValueMapper.selectList(new LambdaQueryWrapper<IndicatorsValue>().eq(IndicatorsValue::getFyear, dto.getYear()).eq(IndicatorsValue::getFmonth, "00")
                .eq(IndicatorsValue::getOrgId, dto.getOrgId()));
        //年度
        List<BaseBusinessAnnual> baseBusinessAnnuals = baseBusinessAnnualService.list(new LambdaQueryWrapper<BaseBusinessAnnual>().eq(BaseBusinessAnnual::getFillingYear, dto.getYear())
                .eq(BaseBusinessAnnual::getFillingUnitId, dto.getOrgId()));
        if (CollectionUtil.isNotEmpty(baseBusinessAnnuals)) {
            BaseBusinessAnnual baseBusinessAnnual = baseBusinessAnnuals.get(0);
            List<BusinessAnnual> businessAnnuals = businessAnnualService.list(new LambdaQueryWrapper<BusinessAnnual>().eq(BusinessAnnual::getBaseBusinessAnnualId, baseBusinessAnnual.getId()));
            businessAnnuals.forEach(businessAnnual -> {
                BusinessAudit businessAudit = new BusinessAudit();
                //筛选财务绩效表Id
                List<BusinessIndicators> indicators = businessIndicators.stream().filter(indicator -> businessAnnual.getBusinessIndicatorsId().equals(indicator.getId())).collect(Collectors.toList());
                if (CollectionUtil.isNotEmpty(indicators)) {
                    BusinessIndicators indicator = indicators.get(0);
                    businessAudit.setBusinessIndicatorsId(businessAnnual.getBusinessIndicatorsId());
                    businessAudit.setIndicatorsCategoryId(indicator.getIndicatorsCategoryId());
                    businessAudit.setAnnualSubmit(businessAnnual.getAnnualSubmit());
                    businessAudit.setIsDeleted(0);
                    //todo 审定认定
                    List<AppraisalIndex> appraisalIndexList = appraisalIndices.stream().filter(m -> m.getId().equals(indicator.getIndicatorsCategoryId())).collect(Collectors.toList());
                    if (CollectionUtil.isNotEmpty(appraisalIndexList)) {
                        AppraisalIndex appraisalIndex = appraisalIndexList.get(0);
                        if (Optional.ofNullable(appraisalIndex.getFlag()).isPresent() && appraisalIndex.getFlag() == 0) {
                            String reviewCode = appraisalIndex.getReviewCode();
                            String indictId = indicatorsMapper.selectOne(new LambdaQueryWrapper<FinanceIndicators>().eq(FinanceIndicators::getCode, reviewCode)).getId();
                            if (CollectionUtil.isNotEmpty(indicatorsValues)) {
                                List<IndicatorsValue> values = indicatorsValues.stream().filter(value -> indictId.equals(value.getIndicatorsId())).collect(Collectors.toList());
                                if (CollectionUtil.isNotEmpty(values)) {
                                    Optional<IndicatorsValue> indicatorsValue = values.stream().filter(m -> "1".equals(m.getIsGroup())).findFirst();
                                    if (indicatorsValue.isPresent()) {
                                        BigDecimal fvalue = indicatorsValue.get().getFvalue();
                                        businessAudit.setAuditFirmly(fvalue);
                                    } else {
                                        BigDecimal fvalue = values.get(0).getFvalue();
                                        businessAudit.setAuditFirmly(fvalue);
                                    }
                                }
                            }
                        }
                    }

                }
                businessAuditVOS.add(businessAudit);
            });
        }
        return businessAuditVOS;
    }

    /**
     * @Author: Take-off
     * @Description: //TODO 履职成效 计算小计
     * @Date: 1:17 PM 2023/2/5
     * @Param: [performanceReportVOS]
     * @return: com.plian.system.vo.pf.PerformanceReportVO
     **/
    private List<PerformanceReportVO> getCountValue(List<PerformanceReportVO> allPerformanceReportVOS) {
        List<PerformanceReportVO> reportVOS = new ArrayList<>();
        Map<String, List<PerformanceReportVO>> mapByOrg = allPerformanceReportVOS.stream().collect(Collectors.groupingBy(PerformanceReportVO::getOrgId));
        for (String orgId : mapByOrg.keySet()) {
            PerformanceReportVO countValue = PerformanceReportVO.builder().firstIndex("履职成效小计得分").enterpriseScore(BigDecimal.ZERO).enterpriseResult(BigDecimal.ZERO).officeScore(BigDecimal.ZERO)
                    .officeResult(BigDecimal.ZERO).assetScore(BigDecimal.ZERO).assetResult(BigDecimal.ZERO).orgId(orgId).build();
            List<PerformanceReportVO> performanceReportVOS = mapByOrg.get(orgId);
            if (CollectionUtil.isNotEmpty(performanceReportVOS)) {
                countValue.setEnterpriseScore(
                        addBigDecimal(
                                performanceReportVOS.stream().map(PerformanceReportVO::getEnterpriseScore).filter(m -> Optional.ofNullable(m).isPresent()).collect(Collectors.toList())
                        )
                );

                countValue.setEnterpriseResult(
                        addBigDecimal(
                                performanceReportVOS.stream().map(PerformanceReportVO::getEnterpriseResult).filter(m -> Optional.ofNullable(m).isPresent()).collect(Collectors.toList())
                        )
                );

                countValue.setOfficeScore(
                        addBigDecimal(
                                performanceReportVOS.stream().map(PerformanceReportVO::getOfficeScore).filter(m -> Optional.ofNullable(m).isPresent()).collect(Collectors.toList())
                        )
                );

                countValue.setOfficeResult(
                        addBigDecimal(
                                performanceReportVOS.stream().map(PerformanceReportVO::getOfficeResult).filter(m -> Optional.ofNullable(m).isPresent()).collect(Collectors.toList())
                        )
                );

                countValue.setAssetScore(
                        addBigDecimal(
                                performanceReportVOS.stream().map(PerformanceReportVO::getAssetScore).filter(m -> Optional.ofNullable(m).isPresent()).collect(Collectors.toList())
                        )
                );

                countValue.setAssetResult(
                        addBigDecimal(
                                performanceReportVOS.stream().map(PerformanceReportVO::getAssetResult).filter(m -> Optional.ofNullable(m).isPresent()).collect(Collectors.toList())
                        )
                );
            }
            reportVOS.add(countValue);
        }
        return reportVOS;
    }

    private List<BusinessIndicatorsReportVO> getBusinessCountValue(List<BusinessIndicatorsReportVO> allBusinessIndicatorsReportVOS) {
        List<BusinessIndicatorsReportVO> reportVOS = new ArrayList<>();
        Map<String, List<BusinessIndicatorsReportVO>> mayByOrgId = allBusinessIndicatorsReportVOS.stream().collect(Collectors.groupingBy(BusinessIndicatorsReportVO::getOrgId));
        for (String orgId : mayByOrgId.keySet()) {
            List<BusinessIndicatorsReportVO> businessIndicatorsReportVOS = mayByOrgId.get(orgId);
            BusinessIndicatorsReportVO countValue = new BusinessIndicatorsReportVO();
            countValue.setFirstIndex("经营业绩小计得分");
            countValue.setOrgId(orgId);
            if (CollectionUtil.isNotEmpty(businessIndicatorsReportVOS)) {
//                countValue.setEnterpriseScore(
//                        addBigDecimal(
//                                businessIndicatorsReportVOS.stream().map(BusinessIndicatorsReportVO::getEnterpriseScore).filter(m -> Optional.ofNullable(m).isPresent()).collect(Collectors.toList())
//                        )
//                );

                countValue.setEnterpriseItem(
                        addBigDecimal(
                                businessIndicatorsReportVOS.stream().map(BusinessIndicatorsReportVO::getEnterpriseItem).filter(m -> Optional.ofNullable(m).isPresent()).collect(Collectors.toList())
                        )
                );


                countValue.setEnterpriseResult(
                        addBigDecimal(
                                businessIndicatorsReportVOS.stream().map(BusinessIndicatorsReportVO::getEnterpriseResult).filter(m -> Optional.ofNullable(m).isPresent()).collect(Collectors.toList())
                        )
                );

//                countValue.setOfficeScore(
//                        addBigDecimal(
//                                businessIndicatorsReportVOS.stream().map(BusinessIndicatorsReportVO::getOfficeScore).filter(m -> Optional.ofNullable(m).isPresent()).collect(Collectors.toList())
//                        )
//                );

                countValue.setOfficeItem(
                        addBigDecimal(
                                businessIndicatorsReportVOS.stream().map(BusinessIndicatorsReportVO::getOfficeItem).filter(m -> Optional.ofNullable(m).isPresent()).collect(Collectors.toList())
                        )
                );

                countValue.setOfficeResult(
                        addBigDecimal(
                                businessIndicatorsReportVOS.stream().map(BusinessIndicatorsReportVO::getOfficeResult).filter(m -> Optional.ofNullable(m).isPresent()).collect(Collectors.toList())
                        )
                );

//                countValue.setAssetScore(
//                        addBigDecimal(
//                                businessIndicatorsReportVOS.stream().map(BusinessIndicatorsReportVO::getAssetScore).filter(m -> Optional.ofNullable(m).isPresent()).collect(Collectors.toList())
//                        )
//                );

                countValue.setAssetItem(
                        addBigDecimal(
                                businessIndicatorsReportVOS.stream().map(BusinessIndicatorsReportVO::getAssetItem).filter(m -> Optional.ofNullable(m).isPresent()).collect(Collectors.toList())
                        )
                );

                countValue.setAssetResult(
                        addBigDecimal(
                                businessIndicatorsReportVOS.stream().map(BusinessIndicatorsReportVO::getAssetResult).filter(m -> Optional.ofNullable(m).isPresent()).collect(Collectors.toList())
                        )
                );
            }
            reportVOS.add(countValue);
        }
        return reportVOS;
    }

    /**
     * @Author: Take-off
     * @Description: //TODO BigDecimal 集合相加
     * @Date: 1:13 PM 2023/2/5
     * @Param: [sumValue]
     * @return: java.math.BigDecimal
     **/
    private BigDecimal addBigDecimal(List<BigDecimal> sumValue) {
        BigDecimal sum = BigDecimal.ZERO;
        if (CollectionUtil.isNotEmpty(sumValue)) {
            for (BigDecimal value : sumValue) {
                sum = sum.add(value);
            }
        }
        return sum;
    }

    /**
     * @Author: Take-off
     * @Description: //TODO 计算履职得分
     * @Date: 5:44 PM 2023/2/3
     * @Param: [reportVO, weight, indicatorsId, evaluationScores, officeScores, assessScores, scoreDetails]
     * @return: void
     **/
    private void setScore(PerformanceReportVO reportVO, BigDecimal weight, Long indicatorsId, List<BaseEvaluationScore> evaluationScores, List<BaseOfficeScore> officeScores
            , List<BaseAssessScore> assessScores, List<EvaluationScoreDetail> scoreDetails) {
        //企业
        if (CollectionUtil.isNotEmpty(evaluationScores)) {
            List<Long> baseIds = evaluationScores.stream().map(BaseEvaluationScore::getId).collect(Collectors.toList());
            List<EvaluationScoreDetail> evaluationDetails = scoreDetails.stream().filter(detail -> indicatorsId.equals(detail.getBaseBusinessIndicatorsId()) && baseIds.contains(detail.getBaseScoreId())).collect(Collectors.toList());
            setScoreResult(reportVO, weight, evaluationDetails, 1);
        }
        //责任处室
        if (CollectionUtil.isNotEmpty(officeScores)) {
            List<Long> baseIds = officeScores.stream().map(BaseOfficeScore::getId).collect(Collectors.toList());
            List<EvaluationScoreDetail> evaluationDetails = scoreDetails.stream().filter(detail -> indicatorsId.equals(detail.getBaseBusinessIndicatorsId()) && baseIds.contains(detail.getBaseScoreId())).collect(Collectors.toList());
            setScoreResult(reportVO, weight, evaluationDetails, 2);
        }
        //国资
        if (CollectionUtil.isNotEmpty(assessScores)) {
            List<Long> baseIds = assessScores.stream().map(BaseAssessScore::getId).collect(Collectors.toList());
            List<EvaluationScoreDetail> evaluationDetails = scoreDetails.stream().filter(detail -> indicatorsId.equals(detail.getBaseBusinessIndicatorsId()) && baseIds.contains(detail.getBaseScoreId())).collect(Collectors.toList());
            setScoreResult(reportVO, weight, evaluationDetails, 3);
        }
    }

    /**
     * @Author: Take-off
     * @Description: //TODO 计算得分
     * @Date: 5:43 PM 2023/2/3
     * @Param: [reportVO, weight, evaluationDetails]
     * @return: void
     **/
    private void setScoreResult(PerformanceReportVO reportVO, BigDecimal weight, List<EvaluationScoreDetail> evaluationDetails, Integer type) {
        if (CollectionUtil.isNotEmpty(evaluationDetails)) {
            EvaluationScoreDetail detail = evaluationDetails.get(0);
            BigDecimal score = BigDecimal.ZERO;
            BigDecimal decimal = BigDecimal.ZERO;
            //折合
            if (Optional.ofNullable(detail.getScore()).isPresent()) {
                score = detail.getScore().setScale(4, RoundingMode.HALF_UP);
                if (Optional.ofNullable(weight).isPresent()) {
                    decimal = score.multiply(weight).divide(new BigDecimal(100), 4, RoundingMode.HALF_UP);
                } else {
                    decimal = score;
                }
            }
            if (type == 1) {
                reportVO.setEnterpriseScore(score);
                reportVO.setEnterpriseResult(decimal);
            } else if (type == 2) {
                reportVO.setOfficeScore(score);
                reportVO.setOfficeResult(decimal);
            } else if (type == 3) {
                reportVO.setAssetScore(score);
                reportVO.setAssetResult(decimal);
            }

        }
    }

}
